import 'package:ai_data_capture/main.dart';
import 'package:ai_data_capture/page/tasklist/add_template/logic.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'logic.dart';

class TaskListPage extends StatelessWidget {
  const TaskListPage({super.key});

  @override
  Widget build(BuildContext context) {
    final TaskListLogic logic = Get.find<TaskListLogic>();
    final state = logic.state;
    return Scaffold(
        appBar: AppBar(title: const Text("任务列表")),
        body: SingleChildScrollView(
            child: Column(children: [
          ExpansionTile(
              initiallyExpanded: true,
              title: const Text("执行队列"),
              children: [
                Obx(() {
                  if (state.taskList.isEmpty) {
                    return const Text("无任务运行");
                  } else {
                    return Column(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            InkWell(
                              onTap: logic.removetask,
                              child: const Padding(
                                padding: EdgeInsets.all(8.0),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.end,
                                  children: [
                                    Icon(Icons.remove_circle_outline,
                                        color: Colors.red),
                                    SizedBox(width: 3),
                                    Text("删除已完成的任务",
                                        style: TextStyle(
                                            color: Colors.red, fontSize: 12))
                                  ],
                                ),
                              ),
                            ),
                          ],
                        ),
                        Obx(() {
                          return ListView.builder(
                              shrinkWrap: true,
                              physics: const NeverScrollableScrollPhysics(),
                              itemCount: state.taskList.length,
                              itemBuilder: (BuildContext context, int index) {
                                var bean = state.taskList[index];
                                return Container(
                                    margin: const EdgeInsets.all(8),
                                    padding: const EdgeInsets.all(15),
                                    decoration: BoxDecoration(
                                        color: Colors.white,
                                        borderRadius: BorderRadius.circular(5),
                                        boxShadow: [
                                          BoxShadow(
                                              color: Colors.grey.shade300,
                                              blurRadius: 5,
                                              spreadRadius: 5)
                                        ]),
                                    child: DefaultTextStyle(
                                      style: const TextStyle(
                                          fontSize: 13, color: Colors.black),
                                      child: Row(children: [
                                        Expanded(
                                            child: Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                children: [
                                              Text("任务描述:${bean.description}"),
                                              Text("开始时间:${bean.startTime}"),
                                              Text("结束时间:${bean.endTime}"),
                                              Text("是否完成:${bean.isFinish}")
                                            ])),
                                        Text(bean.isRunning == true
                                            ? '正在运行'
                                            : '没有运行')
                                      ]),
                                    ));
                              });
                        }),
                      ],
                    );
                  }
                })
              ]),
          ExpansionTile(
              initiallyExpanded: true,
              title: const Text("模版列表"),
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    InkWell(
                      onTap: () {
                        Get.toNamed(RoutePath.addTemplate,
                                arguments: AddTempBean(
                                    isAdd: true,
                                    configFileBean: null,
                                    flowBean: null,
                                    desc: '',
                                    id: null))
                            ?.then((r) {
                          logic.refreshList();
                        });
                      },
                      child: const Padding(
                        padding: EdgeInsets.all(8.0),
                        child: Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              Icon(Icons.add_circle_outline,
                                  color: Colors.blue),
                              SizedBox(width: 3),
                              Text("添加模版",
                                  style: TextStyle(
                                      color: Colors.blue, fontSize: 12))
                            ]),
                      ),
                    ),
                  ],
                ),
                Obx(() {
                  if (state.templates.isEmpty) {
                    return const Text("暂无模版");
                  }
                  return Obx(() {
                    return ListView.builder(
                        shrinkWrap: true,
                        physics: const NeverScrollableScrollPhysics(),
                        itemCount: state.templates.length,
                        itemBuilder: (BuildContext context, int index) {
                          var bean = state.templates[index];
                          return Container(
                              margin: const EdgeInsets.all(8),
                              padding: const EdgeInsets.all(15),
                              decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(5),
                                  boxShadow: [
                                    BoxShadow(
                                        color: Colors.grey.shade300,
                                        blurRadius: 5,
                                        spreadRadius: 5)
                                  ]),
                              child: DefaultTextStyle(
                                style: TextStyle(
                                    fontSize: 13, color: Colors.black),
                                child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Text("模版描述:${bean.description}"),
                                      Text("配置文件:${bean.config_desc}"),
                                      Text("流程步骤:${bean.flow_step}"),
                                      Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.spaceBetween,
                                          children: [
                                            InkWell(
                                                onTap: () {
                                                  logic.changeTemplate(bean);
                                                },
                                                child: const Padding(
                                                    padding:
                                                        EdgeInsets.all(8.0),
                                                    child: Column(children: [
                                                      Icon(Icons.edit_note),
                                                      Text("修改模版")
                                                    ]))),
                                            InkWell(
                                                onTap: () {
                                                  logic.addToChannel(bean);
                                                },
                                                child: const Padding(
                                                    padding:
                                                        EdgeInsets.all(8.0),
                                                    child: Column(children: [
                                                      Icon(
                                                          Icons.add_road_sharp),
                                                      Text("加入队列")
                                                    ]))),
                                            InkWell(
                                                onTap: () {
                                                  logic.deleteTemplate(
                                                      "${bean.id}");
                                                },
                                                child: const Padding(
                                                    padding:
                                                        EdgeInsets.all(8.0),
                                                    child: Column(children: [
                                                      Icon(
                                                          Icons
                                                              .delete_forever_outlined,
                                                          color: Colors.red),
                                                      Text("删除模版")
                                                    ])))
                                          ])
                                    ]),
                              ));
                        });
                  });
                })
              ])
        ])));
  }
}
